package com.asyf.demo.service.impl;

import com.asyf.demo.annotation.MyDS;
import com.asyf.demo.entity.User;
import com.asyf.demo.entity.User2;
import com.asyf.demo.mapper.User2Mapper;
import com.asyf.demo.mapper.UserMapper;
import com.asyf.demo.service.DemoService;
import com.asyf.demo.service.DemoService2;
import com.asyf.demo.service.MyBatisPlusService;
import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author yjz
 * @Date 2022/10/28 13:04
 */
@Service
@Slf4j
public class MyBatisPlusServiceImpl implements MyBatisPlusService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private User2Mapper userMapper2;

    @Resource
    private DemoService2 demoService2;

    @Resource
    private DemoService demoService;

    /**
     * 在同一个事务下，DS是无法实现切换数据源的（DS失效)
     * 如果加了@Transaction 注解，就要考虑是否会失效
     *
     * @param num
     */
//    @Transactional
    @Override
    public void saveTest(Integer num) {
        User user = new User();
        user.setName("用户名");
        saveTest1(num);
        saveTest2(num);
    }

    @DS("slave2")
    public void saveTest1(Integer num) {
        User user = new User();
        user.setName("用户名");
        userMapper.insert(user);
//        log.info(String.valueOf(2 / num));
    }

    //    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @DS("master")
    public void saveTest2(Integer num) {
        User user = new User();
        user.setName("用户名");
        userMapper.insert(user);
        log.info(String.valueOf(2 / num));
    }

    /**
     * 必须master库方法先执行，才能回滚，达到事务效果
     * <p>demoService.save方法上不用加事务
     * demoService2.save加上@Transactional(propagation = Propagation.REQUIRES_NEW)事务才能生效，数据源才能切换
     * 原因：
     * 1代理方法在同一个类不行
     * 2新的事务代理会变成新的，这样会有2个事务
     */
    @Transactional
    @Override
    public void saveTest3(Integer num) {
        demoService.save(num);//mysql
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        demoService2.save(num);//oracle
    }

    @MyDS()
    @Override
    public List<User> test5(Integer num) {
        List<User> userList = userMapper.selectList(null);
        return userList;
    }

    @MyDS
    @Override
    public void test6(Integer num) {
        User2 user = new User2();
        user.setName("用户名");
        user.setPassword("密码");
        userMapper.insert(user);
        log.info("自增主键取值:{}", user.getId());
    }

    @MyDS
    @Override
    public Long test7(Integer num) {
        User2 user = new User2();
        user.setName("用户名");
//        user.setPassword("密码");
        userMapper2.insert(user);
        log.info("自增主键取值:{}", user.getId());
        return user.getId();
    }


    @Transactional //去掉事务之后方法中的延时不会占用数据库连接
    @Override
    public void test9(Integer num) {
        User userById = userMapper.findUserById(String.valueOf(num));
        log.info("{}", userById);
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
